package fl.android.ui;

import javax.microedition.khronos.opengles.GL10;

import fl.android.Note;
import fl.android.Project;
import fl.android.Note.Voice;

public class Button {

	public float x, y, width, height;
	public boolean[] selected = new boolean[4];

	public Note[] notes = new Note[4];

	public Button(float x, float y, float width, float height, int position,
			int frequency) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		for (int i = 0; i < 4; i++) {
			notes[i] = new Note(Voice.values()[i], position, frequency);
		}
	}

	public void draw(GL10 gl) {
		int count = 0;
		for (int i = 0; i < 4; i++)
			if (selected[i])
				count++;
		if (count == 0)
			Rect.render(gl, x, y, 0, width, height, new float[] { .5f, .5f,
					.5f, 1f });
		int cur = 0;
		for (int i = 0; i < 4; i++) {
			if (!selected[i]) continue;
			float[] color = UIRenderer.colors[i];
			Rect.render(gl, x, y + cur * height / count, 0, width, height / count,
					color);
			cur++;
		}
	}

	public void toggle() {
		int index = Project.selected.ordinal();
		if (!selected[index]) {
			Project.add(notes[index]);
		} else {
			Project.remove(notes[index]);
		}
		selected[index] = !selected[index];
	}
}
